package org.sanelli.InterstellarGolf.SpecificObject;

import java.util.ArrayList;

import org.sanelli.InterstellarGolf.R;
import org.sanelli.InterstellarGolf.GenericObject.ActivityConstant;
import org.sanelli.InterstellarGolf.GenericObject.BoundingBox;
import org.sanelli.InterstellarGolf.GenericObject.GameEntity;
import org.sanelli.InterstellarGolf.GenericObject.Vector2D;
import org.sanelli.InterstellarGolf.Util.Scaling;
import org.sanelli.SaveTheShip.Function.PhysicFunction;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class GamePlayer extends GameEntity {
	private Vector2D _speed;
	
	public GamePlayer(Context context) {
		super();
		_speed = null;
	}
	
	public GamePlayer(Context context, Vector2D position) {
		super();
		this._position = position;
		this._bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.pallina);
		this._bitmap = Bitmap.createScaledBitmap(_bitmap, 50, 50, false);
		this._bb = new BoundingBox(position.getX() - this._bitmap.getWidth()/2, position.getY() - this._bitmap.getHeight()/2, position.getX() + this._bitmap.getWidth()/2, position.getY() + this._bitmap.getHeight()/2);
	}
	
	@Override
	public void setPosition(Vector2D position) {
		super.setPosition(position);
		this._bb = new BoundingBox(position.getX() - this._bitmap.getWidth()/2, position.getY() - this._bitmap.getHeight()/2, position.getX() + this._bitmap.getWidth()/2, position.getY() + this._bitmap.getHeight()/2);
	}
	
	public Vector2D getSpeed() {
		return _speed;
	}

	public void setSpeed(Vector2D _speed) {
		this._speed = _speed;
	}

	public void updatePhysic(ArrayList<GameNPC> npc, long timelastFrame) {
		Vector2D acceleration = PhysicFunction.total_acceleration(this, npc);
		
		this._speed.setX(this._speed.getX() + acceleration.getX()*timelastFrame*ActivityConstant.TIME_DELTA);
		this._speed.setY(this._speed.getY() + acceleration.getY()*timelastFrame*ActivityConstant.TIME_DELTA);
		this._position.setX(this._position.getX() + this._speed.getX()*timelastFrame*ActivityConstant.TIME_DELTA);
		this._position.setY(this._position.getY() + this._speed.getY()*timelastFrame*ActivityConstant.TIME_DELTA);
		this._bb = new BoundingBox(_position.getX() - this._bitmap.getWidth()/2, this._position.getY() - this._bitmap.getHeight()/2, this._position.getX() + this._bitmap.getWidth()/2, this._position.getY() + this._bitmap.getHeight()/2);
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Vector2D where = Scaling.WorldToScreen(this._bb.getP1());
		canvas.drawBitmap(this._bitmap, (int)where.getX(), (int)where.getY(), null);
	}
	
	@Override
	public String toString() {
		return "Position " + this._position.toString() + " Speed " + this._speed.toString();
	}
}
